package com.raja.servingStation.controller.FirstInsuranceOrder;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.util.RequestUtil;
import com.raja.servingStation.common.util.ResponseUtil;
import com.raja.servingStation.controller.common.BaseController;
import com.raja.servingStation.entity.CarInfoEx;
import com.raja.servingStation.service.FirstInsuranceOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangzhen
 * on 2017/10/10.
 */
@RestController
public class FirstInsuranceOrderController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(FirstInsuranceOrderController.class);
    @Autowired
    private ResponseUtil responseUtil;
    @Autowired
    private FirstInsuranceOrderService firstInsuranceOrderService;


    /**
     * 新增首保单
     * @param request
     * @return
     */
    @RequestMapping(value = "/firstInsuranceOrder/add",method = RequestMethod.GET)
    public ResponseEntity add(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Object id = firstInsuranceOrderService.addFirstInsuranceOrder(body);
            if(id.equals("手机号不合法！")){
                return responseUtil.exception("手机号不合法！");
            }
            if(id.equals("身份证不合法！")){
                return responseUtil.exception("身份证不合法！");
            }
            if(id.equals("座机号不合法！")){
                return responseUtil.exception("座机号不合法！");
            }
            if(id.equals("一辆车只能有一次首保!")){
                return responseUtil.exception("一辆车只能有一次首保!");
            }
            return responseUtil.success(id);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("新增完工首保单异常");
        }
    }

    @RequestMapping(value = "/firstInsuranceOrder/submitSbOrder",method = RequestMethod.GET)
    public ResponseEntity submitSbOrder(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Object id = firstInsuranceOrderService.submitSbOrder(body);
            if(id.equals("一辆车只能有一次首保!")){
                return responseUtil.exception("一辆车只能有一次首保!");
            }
            return responseUtil.success(id);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("提交首保单异常");
        }
    }

    @RequestMapping(value = "/firstInsuranceOrder/getCarInfoByCarNo",method = RequestMethod.GET)
    public ResponseEntity getCarInfoByCarNo(HttpServletRequest request) {
        try {
            JSONObject body = RequestUtil.parseBody(request);
            CarInfoEx carInfoEx = firstInsuranceOrderService.getCarInfoByCarNo(body);
            return responseUtil.success(carInfoEx);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return responseUtil.exception("查询列表异常");
        }
    }


}
